package com.zhuiyun.project.api.system.businesPost.service.xml;

import com.zhuiyun.project.api.system.businesPost.entity.SysPost;
import com.zhuiyun.project.api.system.businesPost.mapper.SysBusinesPostMapper;
import com.zhuiyun.project.api.system.businesPost.service.SysBusinesPostService;
import com.zhuiyun.project.common.constant.UserConstants;
import com.zhuiyun.project.common.exception.ServiceException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SysBusinesPostServiceImpl
 * @Description TODO 租户岗位管理服务层实现
 * @Author wx
 * @Date 2023/12/6 15:41
 **/
@Service
public class SysBusinesPostServiceImpl implements SysBusinesPostService {
    @Autowired
    private SysBusinesPostMapper sysBusinesPostMapper;

    /**
     * @Author wx
     * @Description 获取岗位列表
     * @Date 2023/12/6 15:47
     * @Return
     * @param post
     */
    @Override
    public CommonResult selectPostList(SysPost post, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<SysPost> sysPosts = sysBusinesPostMapper.selectPostList(post);
        return CommonResult.ok(MybatisPageHelper.getPageResult(sysPosts));
    }

    /**
     * @Author wx
     * @Description 查询岗位名称是否存在
     * @Date 2023/12/6 16:02
     * @Return
     * @param post
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = sysBusinesPostMapper.checkPostNameUnique(post.getPostName(),post.getVenueUniqueIdentifier());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * @Author wx
     * @Description 查询岗位编号是否存在
     * @Date 2023/12/6 16:05
     * @Return
     * @param post
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = sysBusinesPostMapper.checkPostCodeUnique(post.getPostCode(),post.getVenueUniqueIdentifier());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * @Author wx
     * @Description 新增岗位信息
     * @Date 2023/12/6 16:08
     * @Return
     * @param post
     */
    @Override
    public int insertPost(SysPost post) {
        return sysBusinesPostMapper.insertPost(post);
    }

    /**
     * @Author wx
     * @Description 根据岗位编号获取详细信息
     * @Date 2023/12/6 16:15
     * @Return
     * @param postId
     */
    @Override
    public SysPost selectPostById(Long postId, String venueUniqueIdentifier) {
        return sysBusinesPostMapper.selectPostById(postId,venueUniqueIdentifier);
    }

    /**
     * @Author wx
     * @Description 修改岗位信息
     * @Date 2023/12/6 16:23
     * @Return
     * @param post
     */
    @Override
    public int updatePost(SysPost post) {
        return sysBusinesPostMapper.updatePost(post);
    }

    /**
     * @Author wx
     * @Description 删除岗位
     * @Date 2023/12/6 16:27
     * @Return
     * @param postIds
     */
    @Override
    public int deletePostByIds(Long[] postIds,String venueUniqueIdentifier) {
        for (Long postId : postIds)
        {
            SysPost post = selectPostById(postId,venueUniqueIdentifier);
            if (countUserPostById(postId,venueUniqueIdentifier) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return sysBusinesPostMapper.deletePostByIds(postIds,venueUniqueIdentifier);
    }

    /**
     * @Author wx
     * @Description 查询所有岗位
     * @Date 2023/12/8 8:31
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public List<SysPost> selectPostAll(String venueUniqueIdentifier) {
        return sysBusinesPostMapper.selectPostAll(venueUniqueIdentifier);
    }

    /**
    * @Author wx
    * @Description 根据id获取岗位选择列表
    * @Date 2023/12/8 8:47
    * @Return
    * @param userId
    */
    @Override
    public List<Long> selectPostListByUserId(long userId, String venueUniqueIdentifier) {
        return sysBusinesPostMapper.selectPostListByUserId(userId,venueUniqueIdentifier);
    }

    /**
    * @Author wx
    * @Description 根据id查询岗位使用情况
    * @Date 2023/12/6 16:40
    * @Return
    * @param postId
    */
    private int countUserPostById(Long postId, String venueUniqueIdentifier) {
        return sysBusinesPostMapper.countUserPostById(postId,venueUniqueIdentifier);
    }
}
